<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>部门列表</title>
<script type="text/javascript">
    var newDepartmentWin;
    var editDepartmentWin;
    var departmentTree;
    Ext.onReady(function () {
        Ext.define('DepartmentModel', {
            extend: 'Ext.data.Model',
            fields: [
                {name: 'id', type: 'int'},
                {name: 'number', type: 'string'},
                {name: 'company_name', type: 'string'},
                {name: 'company_id', type: 'int'},
                {name: 'name', type: 'string'},
                {name: 'sub_type', type: 'string'},
                {name: 'is_locked', type: 'string'},
                {name: 'description'},
                {name: 'created_at'},
                {name: 'updated_at'}
            ]
        });

        var deptStore = Ext.create('Ext.data.TreeStore', {
            //autoDestroy : true,
            model: 'DepartmentModel',
            proxy: {
                type: 'ajax',
                url: '/department/getAllDepartmentsForTree'
            },
            root: {
                name: '所有部门',
                id: '0',
                expanded: true
            }//,
            //folderSort: true
        });


        var companyId = Ext.create('Ext.form.field.Hidden', {
            value: '<%=session[:company_id]%>'
        });

        var companies = Ext.create('Ext.form.ComboBox', {
            labelWidth: 65,
            fieldLabel: '所属公司',
            store: companyAllInfoStore,
            allowBlank: false,
            blankText: "公司必须选择",
            emptyText: '请选择...',
            valueField: 'id',
            displayField: 'name',
            name: 'company_id',
            loadingText: '搜索中...',
            minChars: 0,
            anchor: '100%',
            listConfig: {
                loadingText: 'Searching...',
                getInnerTpl: function () {
                    return '{name}';
                }
            },
            pageSize: 10
        });


        var searchButton = Ext.create('Ext.Button', {
            text: '搜索',
            iconCls: 'finding',
            handler: function(){
                //deptStore.removeAll();
                deptStore.getProxy().extraParams = queryFormPanel.getForm().getFieldValues();
                deptStore.load();
                companyId = companies.getValue();
            }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
            fieldDefaults: {
                labelWidth: 60,
                labelAlign: 'right'
            },
            width: 450,
            frame: true,
            border: false,
            items: [
                {
                    xtype: 'fieldset',
                    title: '查询条件',
                    frame: true,
                    items: [
                        {
                            xtype: 'container',
                            anchor: '100%',
                            layout: 'column',
                            items: [
                                {
                                    columnWidth: .80,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [companies]
                                },
                                {
                                    columnWidth: .20,
                                    layout: 'anchor',
                                    xtype: 'container',
                                    items: [searchButton]
                                }
                            ]
                        }
                    ]
                }
            ]
        });



        departmentTree = Ext.create('Ext.tree.Panel', {
            width: screenWidth - 200,
            height: screenHeight - 285,
            store: deptStore,
            rootVisible: true,
            autoScroll: true,
            useArrows: true,
            multiSelect: true,
            singleExpand: true,
            title: '部门(分公司)列表',
            columns: [
                {
                    header: 'DBID',
                    width: 40,
                    dataIndex: 'id'
                },
                {
                    xtype: 'treecolumn',
                    header: '名称',
                    width: 330,
                    dataIndex: 'name'
                },{
                    header: '所属公司',
                    width: 100,
                    dataIndex: 'company_name'
                },
                {
                    header: '编号',
                    width: 100,
                    dataIndex: 'number'
                },
                {
                    header: '类型',
                    width: 60,
                    dataIndex: 'sub_type'
                },
                {
                    header: '是否锁定',
                    width: 100,
                    renderer: renderDeptIsLucked,
                    dataIndex: 'is_locked'
                },
                {
                    header: '备注',
                    width: 300,
                    dataIndex: 'description'
                }
            ],
            bbar: [
                {
                    text: '刷新整树',
                    iconCls: 'refresh',
                    handler: function () {
                        deptStore.load();
                    }
                }
            ]
        });

        //锁定仓库类型
        function renderDeptIsLucked(value, cellmeta, record, index, columnIndex, store) {
            if (record.is_locked == "1") {
                return "<a style=cursor:pointer )><em style=color:red>锁定</em></a>";
            } else {
                return "<a style=cursor:pointer ><em style=color:green>未锁定</em></a>";
            }
        }

        departmentTree.on("itemcontextmenu", function (view, record, item, index, e, options) {
            e.preventDefault();
            DepartmentRightMenu.showAt(e.getXY());
            //DepartmentRightMenu.show(node.ui.getAnchor());
            currentNode = record;
            nodeId = record.get('id');
            if (nodeId == 0 || nodeId == "NaN") {
                editDepartmentMenuItem.setDisabled(true);
                delDepartmentMenuItem.setDisabled(true);
                lockMenuItem.setDisabled(true);
            } else {
                editDepartmentMenuItem.setDisabled(false);
                delDepartmentMenuItem.setDisabled(false);
                lockMenuItem.setDisabled(false);
            }
            refreshMenuItem.setDisabled(false);
        }, this);

        //右键菜单
        var addDepartmentMenuItem = Ext.create('Ext.menu.Item', {
            text: '添加部门(店铺、分公司)',
            iconCls: 'addItem',
            handler: rightMenuDepartmentTreeFn
        });
        var editDepartmentMenuItem = Ext.create('Ext.menu.Item', {
            text: '编辑部门(店铺、分公司)',
            iconCls: 'editItem',
            handler: rightMenuDepartmentTreeFn
        });
        var delDepartmentMenuItem = Ext.create('Ext.menu.Item', {
            text: '删除',
            iconCls: 'remove',
            handler: rightMenuDepartmentTreeFn
        });
        var lockMenuItem = Ext.create('Ext.menu.Item', {
            text: '锁定或解锁',
            iconCls: 'lock',
            handler: rightMenuDepartmentTreeFn
        });
        var refreshMenuItem = Ext.create('Ext.menu.Item', {
            text: '刷新',
            iconCls: 'refresh',
            handler: rightMenuDepartmentTreeFn
        });

        var DepartmentRightMenu = Ext.create('Ext.menu.Menu', {
            //id: 'Department_right_menu',
            items: [
                addDepartmentMenuItem,
                editDepartmentMenuItem,
                '-',
                delDepartmentMenuItem,
                '-',
                lockMenuItem,
                '-',
                refreshMenuItem
            ]});

        function rightMenuDepartmentTreeFn(item, e) {
            if (item.text == "添加部门(店铺、分公司)") {
                newDepartmentWin = Ext.create('Ext.window.Window', {
                    layout: 'fit',
                    width: 318,
                    height: 275,
                    closeAction: 'destroy',
                    plain: true,
                    modal: true,
                    constrain: true,
                    //modal: true,
                    title: '添加部门(店铺、分公司)',
                    autoLoad: {
                        url: "/department/newDepartment?parent_id=" + nodeId,
                        scripts: true
                    }
                });
                newDepartmentWin.on("destroy", function () {
                    //刷新整个树
                    deptStore.load();
                });
                newDepartmentWin.resizable = false;
                newDepartmentWin.show();
            } else if (item.text == "编辑部门(店铺、分公司)") {
                editDepartmentWin = Ext.create('Ext.window.Window', {
                    layout: 'fit',
                    width: 318,
                    height: 275,
                    closeAction: 'destroy',
                    plain: true,
                    modal: true,
                    constrain: true,
                    //modal: true,
                    title: '编辑部门(店铺、分公司)',
                    autoLoad: {
                        url: "/department/editDepartment?id=" + nodeId,
                        scripts: true
                    }
                });
                editDepartmentWin.on("destroy", function () {
                    //刷新整个树
                    deptStore.load();
                });
                editDepartmentWin.resizable = false;
                editDepartmentWin.show();
            } else if (item.text == "刷新") {
                if (currentNode.isLeaf()) {
                    deptStore.load({
                        node: currentNode.parentNode
                    });
                } else {
                    deptStore.load({
                        node: currentNode
                    });
                }
            } else if (item.text == "删除") {
                Ext.MessageBox.confirm("提示", "你确认删除 \"" + currentNode.get('name') + "\" 吗？", function (btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: '/department/destroy?id=' + currentNode.getId(),
                            success: function (result, request) {
                                deptStore.load();
                            },
                            failure: function (result, request) {
                                Ext.MessageBox.show({
                                    title: '消息',
                                    msg: "通讯失败，请从新操作",
                                    buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox.WARNING
                                });
                            },
                            method: 'GET'
                        });
                    } else {
                        //alert("no");
                    }
                });
            } else if (item.text == "锁定或解锁") {
                //处理销售类型锁定
                Ext.Ajax.request({
                    url: '/department/departmentLock',
                    success: function (result, request) {
                        //刷新整个树
                        deptStore.load();
                    },
                    failure: function (result, request) {
                        Ext.MessageBox.show({
                            title: '消息',
                            msg: "通讯失败，请从新操作",
                            buttons: Ext.MessageBox.OK,
                            icon: Ext.MessageBox.WARNING
                        });
                    },
                    method: 'GET',
                    params: {
                        id: currentNode.getId()
                    }
                });
            }
        }

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
            width: screenWidth - 165,
            height: 700,
            frame: false,
            layout: {type: 'table', columns: 1},
            items: [queryFormPanel, departmentTree]
        });

        workbenchPanel.render("departments_tree_div");

    })

</script>
</head>
<body>
<div id="departments_tree_div"></div>
</body>
</html>
